package com.zmn.oms.zmn.business.impl.statement;

import com.zmn.base.channel.common.dro.ChannelSettleDRO;
import com.zmn.base.channel.dubbo.interfaces.channel.settle.ChannelSettleListRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.model.dto.work.modify.must.OrderStatementDTO;
import com.zmn.oms.model.entity.statement.OrderStatement;
import com.zmn.oms.services.interfaces.statement.OrderStatementService;
import com.zmn.oms.zmn.business.interfaces.statement.ZsOrderStatementBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * 类描述： 订单结算信息
 *
 * @author yule
 * @date 2021/3/3 16:07
 */
@Service
@Slf4j
public class ZsOrderStatementBServiceImpl implements ZsOrderStatementBService {
    protected Logger logger = LoggerFactory.getLogger(getClass());


    @Autowired
    private OrderStatementService orderStatementService;
    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private ChannelSettleListRemoteService channelSettleListRemoteService;

    @Override
    public void updateChannelStatementCanTime(Long orderId, Long workId, Integer channelId, Integer companyId) {
        /*
         *  分单/领单时，判断是否满足渠道结算方式，设置渠道可结算时间
         * // 1.分单至家修匠公司(45111)：可结算时间为首次领单时间
         * // 2.分单至非家修匠公司(45111)：可结算时间为首次分单时间
         */
        OrderStatement orderStatement = orderStatementService.findOrderStatementByOrderId(orderId, workId);
        if (Objects.isNull(orderStatement) || orderStatement.getChannelStatementCanTime() == null) {
            // 判断渠道结算方式
            ResponseDTO<ChannelSettleDRO> resp = channelSettleListRemoteService.getByChannelId(channelId);
            logger.info("#oms#判断渠道结算方式getSettleByChannelId入参channelId={},出参resp={}", channelId, resp.getData());
            if (!resp.isSuccess() || Objects.isNull(resp.getData())) {
                return;
            }
            // 1完成 2领单(领单节点结算 设置 渠道可结算时间)
            if (Objects.equals(GlobalConsts.YES, resp.getData().getSettleNode())) {
                OrderStatementDTO orderStatementDTO = new OrderStatementDTO();
                orderStatementDTO.setOrderId(orderId);
                orderStatementDTO.setChannelStatementCanTime(new Date());
                orderStatementService.updateStatement(orderStatementDTO);
            }
        }
    }

}